package com.apkicon.quality;

import java.awt.image.BufferedImage;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

/**
 * 图标质量检测和优化工具
 */
public class IconQualityAnalyzer {
    
    /**
     * 分析图标质量
     */
    public static QualityReport analyzeIcon(BufferedImage image) {
        QualityReport report = new QualityReport();
        
        // 基本尺寸信息
        report.width = image.getWidth();
        report.height = image.getHeight();
        report.isSquare = report.width == report.height;
        
        // 分析颜色分布
        analyzeColors(image, report);
        
        // 分析透明度
        analyzeTransparency(image, report);
        
        // 分析边缘质量
        analyzeEdges(image, report);
        
        // 计算质量评分
        calculateQualityScore(report);
        
        return report;
    }
    
    /**
     * 分析颜色分布
     */
    private static void analyzeColors(BufferedImage image, QualityReport report) {
        int width = image.getWidth();
        int height = image.getHeight();
        
        int totalPixels = width * height;
        int[] colorCounts = new int[256]; // RGB各通道的颜色分布
        
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                Color color = new Color(image.getRGB(x, y), true);
                colorCounts[color.getRed()]++;
                colorCounts[color.getGreen()]++;
                colorCounts[color.getBlue()]++;
            }
        }
        
        // 计算颜色丰富度
        int uniqueColors = 0;
        for (int count : colorCounts) {
            if (count > 0) uniqueColors++;
        }
        
        report.colorRichness = (double) uniqueColors / (256 * 3);
        report.hasGoodColorDistribution = report.colorRichness > 0.1;
    }
    
    /**
     * 分析透明度
     */
    private static void analyzeTransparency(BufferedImage image, QualityReport report) {
        int width = image.getWidth();
        int height = image.getHeight();
        
        int transparentPixels = 0;
        int semiTransparentPixels = 0;
        
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                Color color = new Color(image.getRGB(x, y), true);
                int alpha = color.getAlpha();
                
                if (alpha == 0) {
                    transparentPixels++;
                } else if (alpha < 255) {
                    semiTransparentPixels++;
                }
            }
        }
        
        int totalPixels = width * height;
        report.transparencyRatio = (double) transparentPixels / totalPixels;
        report.semiTransparencyRatio = (double) semiTransparentPixels / totalPixels;
        
        report.hasGoodTransparency = report.transparencyRatio > 0.1 && report.transparencyRatio < 0.9;
    }
    
    /**
     * 分析边缘质量
     */
    private static void analyzeEdges(BufferedImage image, QualityReport report) {
        int width = image.getWidth();
        int height = image.getHeight();
        
        int edgePixels = 0;
        int smoothEdges = 0;
        
        for (int y = 1; y < height - 1; y++) {
            for (int x = 1; x < width - 1; x++) {
                Color center = new Color(image.getRGB(x, y), true);
                
                // 检查周围像素的差异
                boolean isEdge = false;
                int maxDiff = 0;
                
                for (int dy = -1; dy <= 1; dy++) {
                    for (int dx = -1; dx <= 1; dx++) {
                        if (dx == 0 && dy == 0) continue;
                        
                        Color neighbor = new Color(image.getRGB(x + dx, y + dy), true);
                        int diff = Math.abs(center.getRed() - neighbor.getRed()) +
                                  Math.abs(center.getGreen() - neighbor.getGreen()) +
                                  Math.abs(center.getBlue() - neighbor.getBlue());
                        
                        maxDiff = Math.max(maxDiff, diff);
                        
                        if (diff > 50) { // 边缘阈值
                            isEdge = true;
                        }
                    }
                }
                
                if (isEdge) {
                    edgePixels++;
                    if (maxDiff < 100) { // 平滑边缘
                        smoothEdges++;
                    }
                }
            }
        }
        
        int totalPixels = width * height;
        report.edgeRatio = (double) edgePixels / totalPixels;
        report.smoothEdgeRatio = edgePixels > 0 ? (double) smoothEdges / edgePixels : 0;
        
        report.hasGoodEdges = report.edgeRatio > 0.05 && report.smoothEdgeRatio > 0.3;
    }
    
    /**
     * 计算质量评分
     */
    private static void calculateQualityScore(QualityReport report) {
        double score = 0;
        
        // 尺寸评分 (30%)
        if (report.isSquare) score += 30;
        
        // 颜色评分 (25%)
        if (report.hasGoodColorDistribution) score += 25;
        
        // 透明度评分 (25%)
        if (report.hasGoodTransparency) score += 25;
        
        // 边缘评分 (20%)
        if (report.hasGoodEdges) score += 20;
        
        report.qualityScore = score;
        
        if (score >= 80) {
            report.qualityLevel = "优秀";
        } else if (score >= 60) {
            report.qualityLevel = "良好";
        } else if (score >= 40) {
            report.qualityLevel = "一般";
        } else {
            report.qualityLevel = "较差";
        }
    }
    
    /**
     * 质量报告
     */
    public static class QualityReport {
        public int width;
        public int height;
        public boolean isSquare;
        
        public double colorRichness;
        public boolean hasGoodColorDistribution;
        
        public double transparencyRatio;
        public double semiTransparencyRatio;
        public boolean hasGoodTransparency;
        
        public double edgeRatio;
        public double smoothEdgeRatio;
        public boolean hasGoodEdges;
        
        public double qualityScore;
        public String qualityLevel;
        
        @Override
        public String toString() {
            return String.format(
                "图标质量报告:\n" +
                "尺寸: %dx%d (%s)\n" +
                "颜色丰富度: %.2f%% (%s)\n" +
                "透明度: %.2f%% (%s)\n" +
                "边缘质量: %.2f%% (%s)\n" +
                "质量评分: %.1f/100 (%s)",
                width, height, isSquare ? "方形" : "非方形",
                colorRichness * 100, hasGoodColorDistribution ? "良好" : "一般",
                transparencyRatio * 100, hasGoodTransparency ? "良好" : "一般",
                smoothEdgeRatio * 100, hasGoodEdges ? "良好" : "一般",
                qualityScore, qualityLevel
            );
        }
    }
}
